package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

    // Declaring variables
    Socket connectionSocket = null;

    PrintWriter printWriter = null;

    BufferedReader bufferedReader = null, clientInputReader = null;

    final int PORT_NUMBER = 3500;

    final String SERVER_NAME = "127.0.0.1";

    String serverResponse;

    public static void main(String args[]) throws IOException {
        Client clientUsingSockets = new Client();
        clientUsingSockets.start();
    }

    private void start() throws IOException {
        try {
            // Connection to Server
            connectionSocket = new Socket(SERVER_NAME, PORT_NUMBER);
            printWriter = new PrintWriter(connectionSocket.getOutputStream(), true);
            bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            printMessage("Connected to server.");
        } catch (UnknownHostException unknownHostException) {
            printMessage("Host " + SERVER_NAME + " unknown");
            stopServer(1);
        } catch (IOException ioexception) {
            printMessage("Connection failed to host " + SERVER_NAME);
            stopServer(1);
        }

        ClientReader reader = new ClientReader();
        reader.start();

        ClientWriter writer = new ClientWriter();
        writer.start();
    }

    private void printMessage(String message) {
        System.out.println(message);
    }

    private void closeAllOpenStuff() throws IOException {
        printWriter.close();
        bufferedReader.close();
        clientInputReader.close();
        connectionSocket.close();
    }

    private void stopServer(int exitValue) {
        printMessage("Exiting Client process.");
        System.exit(exitValue);
    }

    class ClientReader extends Thread {

        @Override
        public void run() {
            // Reading the response from the server
            try {
                while ((serverResponse = bufferedReader.readLine()) != null) {
                    printMessage(serverResponse);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class ClientWriter extends Thread {

        @Override
        public void run() {
            try {
                clientInputReader = new BufferedReader(new InputStreamReader(System.in));
                String clientInput;

                while ((clientInput = clientInputReader.readLine()) != null) {
                    // Sending the user input to the server
                    printWriter.println(clientInput);
                    if (clientInput.equalsIgnoreCase("Exit") || clientInput.equalsIgnoreCase("Bye") || clientInput.equalsIgnoreCase("Quit"))
                        break;
                }
                closeAllOpenStuff();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
